import echarts from 'echarts'
import testApi from '@/api/testApi'

export default {
  name: 'businessScreen',
  data() {
    return {
      switchValue: true,
      chartDialogVisible: false,
      tempData: '',
      dateRange: '',
      hostNum: '',
      normalHostNum: '',
      abnormalHostNum: '',
      cpuRadio: '云主机',
      memoryRadio: '云主机',
      diskRadio: '云主机',
      abnormalRadio: 'CPU',
      alarmRadio: '全部',
      serviceRadio: 'aaaa',
      pieChartFlag: '',
      pieChartDialogVisible: false,
      pieChartDialogTitle: '',
      pieChartAbnormalList: [],
      tableDetailDialogVisible: false,
      tableDetailMessage: '',
      abnormalData: [],
      gailan_box: [],
      // chartsDatas: [],
      manufacturerChartData: [],
      alarmTableData: [],
      // dialogChartData: [],
      alarmTableDataShow: []
    }
  },
  created() {
  },

  async mounted() {
    await this.getBaseDatas()
    await this.makeUsageCharts()
    await this.getAlarmTableData()
    // this.getDialogChartDatas()
  },
  methods: {
    makeData() {
      let base = +new Date(2023, 5, 10)
      const oneDay = 24 * 3600 * 1000
      const data = [[base, Math.random() * 300]]
      for (let i = 1; i < 2000; i++) {
        const now = new Date((base += oneDay))
        data.push([+now, Math.abs(Math.round((Math.random() - 0.5) * 20 + data[i - 1][1]))])
      }
      return data
    },
    /* getDialogChartDatas() {
      this.chartsDatas = [
        { orgId: 0, orgName: 'ali-region22-1', cpu: 6, memory: 12, disk: 130 },
        { orgId: 11, orgName: 'ali-region22-2', cpu: 4, memory: 16, disk: 319 },
        { orgId: 106, orgName: 'huawei-region37-1', cpu: 12, memory: 1, disk: 325 },
        { orgId: 107, orgName: 'unicom-region24-1', cpu: 0, memory: 9, disk: 288 },
        { orgId: 108, orgName: 'volcano-region33-1', cpu: 45, memory: 2, disk: 255 },
        { orgId: 109, orgName: 'huawei-region37-2', cpu: 0, memory: 8, disk: 198 }
      ]
      for (let i = 0; i < this.chartsDatas.length; i++) {
        const valueCpu = this.makeData()
        const valueMemory = this.makeData()
        const valueDisk = this.makeData()
        const valuePerformance = this.makeData()

        this.dialogChartData.push({ orgName: this.chartsDatas[i].orgName,
          dataListCpu: valueCpu,
          dataListMemory: valueMemory,
          dataListDisk: valueDisk,
          dataListPerformance: valuePerformance
        })
      }
    },*/
    fullScreen() {
      const element = document.getElementById('entirety')
      if (element.requestFullscreen) {
        element.requestFullscreen()
      } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen()
      } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen()
      } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen()
      }
    },
    async makeCpuChart() {
      const statistics_cpu_chart = echarts.init(document.getElementById('statistics_cpu'), 'light')
      let data
      await testApi.getCpuChartData({ cpuType: this.cpuRadio }).then((res) => {
        data = res.data
      }).catch(() => {
        data = [
          { orgId: 0, orgName: 'ali-region22-1', cpu: 6 },
          { orgId: 11, orgName: 'ali-region22-2', cpu: 4 },
          { orgId: 106, orgName: 'huawei-region37-1', cpu: 12 },
          { orgId: 107, orgName: 'unicom-region24-1', cpu: 0 },
          { orgId: 108, orgName: 'volcano-region33-1', cpu: 45 },
          { orgId: 109, orgName: 'huawei-region37-2', cpu: 0 }
        ]
      })
      const maxCpu = Math.max.apply(Math, data.map(item => item.cpu))
      data.sort((a, b) => b.cpu - a.cpu)
      const statistics_cpu_option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.orgName).slice(0, 5).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          // shadow
          type: 'bar',
          itemStyle: {
            normal: { color: 'rgb(255, 255, 255, 0.1)' }
          },
          data: [maxCpu, maxCpu, maxCpu, maxCpu, maxCpu],
          barGap: '-100%',
          barWidth: 10
        }, {
          data: data.map(s => s.cpu).slice(0, 5).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 10,
          left: 10,
          bottom: 10
        }
      }

      statistics_cpu_chart.setOption(statistics_cpu_option)
      statistics_cpu_chart.getZr().on('click', async params => {
        const pointInPixel = [params.offsetX, params.offsetY]
        if (statistics_cpu_chart.containPixel('grid', pointInPixel)) {
          // 点击第几个柱子
          const pointInGrid = statistics_cpu_chart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
          const yIndex = pointInGrid[1]
          data.sort((a, b) => b.cpu - a.cpu)
          const orgName = data.map(s => s.orgName).slice(0, 5).reverse()[yIndex]

          let dialogChartDataUsage = []
          /* for (let i = 0; i < this.dialogChartData.length; i++) {
            if (this.dialogChartData[i].orgName === orgName) {
              dialogChartDataUsage = this.dialogChartData[i].dataListCpu
            }
          }*/

          await testApi.getLineChartByOrgName({ orgName: orgName, type: 'cpu' }).then((res) => {
            dialogChartDataUsage = res.data
          }).catch(() => {
            dialogChartDataUsage = this.makeData()
          })

          this.chartDialogVisible = true
          this.$nextTick(() => {
            const dialogChart = echarts.init(this.$refs.chartInDialog, 'dark')
            const dialogChartOption = {
              tooltip: {
                trigger: 'axis',
                position: function(pt) {
                  return [pt[0], '10%']
                }
              },
              title: {
                left: 'center',
                text: ''
              },
              backgroundColor: 'transparent',
              toolbox: {
                feature: {
                  dataZoom: {
                    yAxisIndex: 'none'
                  },
                  restore: {},
                  saveAsImage: {}
                }
              },
              xAxis: {
                type: 'time',
                boundaryGap: false
              },
              yAxis: {
                type: 'value',
                boundaryGap: [0, '100%']
              },
              dataZoom: [
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                },
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                }
              ],
              series: [
                {
                  name: 'test data',
                  type: 'line',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {},
                  data: dialogChartDataUsage
                }
              ]
            }
            dialogChart.setOption(dialogChartOption)
          })
        }
      })
    },
    async makeMemoryChart() {
      const statistics_memory_chart = echarts.init(document.getElementById('statistics_memory'), 'light')
      let data
      await testApi.getMemoryChartData({ memoryType: this.memoryRadio }).then((res) => {
        data = res.data
      }).catch(() => {
        data = [
          { orgId: 0, orgName: 'ali-region22-1', memory: 12 },
          { orgId: 11, orgName: 'ali-region22-2', memory: 16 },
          { orgId: 106, orgName: 'huawei-region37-1', memory: 1 },
          { orgId: 107, orgName: 'unicom-region24-1', memory: 9 },
          { orgId: 108, orgName: 'volcano-region33-1', memory: 2 },
          { orgId: 109, orgName: 'huawei-region37-2', memory: 8 }
        ]
      })
      const maxMemory = Math.max.apply(Math, data.map(item => item.memory))
      data.sort((a, b) => b.memory - a.memory)
      const statistics_memory_option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.orgName).slice(0, 5).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          // shadow
          type: 'bar',
          itemStyle: {
            normal: { color: 'rgb(255, 255, 255, 0.1)' }
          },
          data: [maxMemory, maxMemory, maxMemory, maxMemory, maxMemory],
          barGap: '-100%',
          barWidth: 10
        }, {
          data: data.map(s => s.memory).slice(0, 5).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 10,
          left: 10,
          bottom: 10
        }
      }
      statistics_memory_chart.setOption(statistics_memory_option)
      statistics_memory_chart.getZr().on('click', async params => {
        const pointInPixel = [params.offsetX, params.offsetY]
        if (statistics_memory_chart.containPixel('grid', pointInPixel)) {
          // 点击第几个柱子
          const pointInGrid = statistics_memory_chart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
          const yIndex = pointInGrid[1]
          data.sort((a, b) => b.memory - a.memory)
          const orgName = data.map(s => s.orgName).slice(0, 5).reverse()[yIndex]

          let dialogChartDataUsage = []
          /* for (let i = 0; i < this.dialogChartData.length; i++) {
            if (this.dialogChartData[i].orgName === orgName) {
              dialogChartDataUsage = this.dialogChartData[i].dataListMemory
            }
          }*/
          await testApi.getLineChartByOrgName({ orgName: orgName, type: 'memory' }).then((res) => {
            dialogChartDataUsage = res.data
          }).catch(() => {
            dialogChartDataUsage = this.makeData()
          })
          this.chartDialogVisible = true
          this.$nextTick(() => {
            const dialogChart = echarts.init(this.$refs.chartInDialog, 'dark')
            const dialogChartOption = {
              tooltip: {
                trigger: 'axis',
                position: function(pt) {
                  return [pt[0], '10%']
                }
              },
              title: {
                left: 'center',
                text: ''
              },
              backgroundColor: 'transparent',
              toolbox: {
                feature: {
                  dataZoom: {
                    yAxisIndex: 'none'
                  },
                  restore: {},
                  saveAsImage: {}
                }
              },
              xAxis: {
                type: 'time',
                boundaryGap: false
              },
              yAxis: {
                type: 'value',
                boundaryGap: [0, '100%']
              },
              dataZoom: [
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                },
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                }
              ],
              series: [
                {
                  name: 'test data',
                  type: 'line',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {},
                  data: dialogChartDataUsage
                }
              ]
            }
            dialogChart.setOption(dialogChartOption)
          })
        }
      })
    },
    async makeDiskChart() {
      const statistics_disk_chart = echarts.init(document.getElementById('statistics_disk'), 'light')
      let data
      await testApi.getDiskChartData({ diskType: this.diskRadio }).then((res) => {
        data = res.data
      }).catch(() => {
        data = [
          { orgId: 0, orgName: 'ali-region22-1', disk: 130 },
          { orgId: 11, orgName: 'ali-region22-2', disk: 319 },
          { orgId: 106, orgName: 'huawei-region37-1', disk: 325 },
          { orgId: 107, orgName: 'unicom-region24-1', disk: 288 },
          { orgId: 108, orgName: 'volcano-region33-1', disk: 255 },
          { orgId: 109, orgName: 'huawei-region37-2', disk: 198 }
        ]
      })
      const maxDisk = Math.max.apply(Math, data.map(item => item.disk))
      data.sort((a, b) => b.disk - a.disk)
      const statistics_disk_option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.orgName).slice(0, 5).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          // shadow
          type: 'bar',
          itemStyle: {
            normal: { color: 'rgb(255, 255, 255, 0.1)' }
          },
          data: [maxDisk, maxDisk, maxDisk, maxDisk, maxDisk],
          barGap: '-100%',
          barWidth: 10
        }, {
          data: data.map(s => s.disk).slice(0, 5).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#307CE0'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 10,
          left: 10,
          bottom: 10
        }
      }

      statistics_disk_chart.setOption(statistics_disk_option)
      statistics_disk_chart.getZr().on('click', async params => {
        const pointInPixel = [params.offsetX, params.offsetY]
        if (statistics_disk_chart.containPixel('grid', pointInPixel)) {
          // 点击第几个柱子
          const pointInGrid = statistics_disk_chart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
          const yIndex = pointInGrid[1]
          data.sort((a, b) => b.disk - a.disk)
          const orgName = data.map(s => s.orgName).slice(0, 5).reverse()[yIndex]
          let dialogChartDataUsage = []
          /* for (let i = 0; i < this.dialogChartData.length; i++) {
            if (this.dialogChartData[i].orgName === orgName) {
              dialogChartDataUsage = this.dialogChartData[i].dataListDisk
            }
          }*/
          await testApi.getLineChartByOrgName({ orgName: orgName, type: 'disk' }).then((res) => {
            dialogChartDataUsage = res.data
          }).catch(() => {
            dialogChartDataUsage = this.makeData()
          })
          this.chartDialogVisible = true
          this.$nextTick(() => {
            const dialogChart = echarts.init(this.$refs.chartInDialog, 'dark')
            const dialogChartOption = {
              tooltip: {
                trigger: 'axis',
                position: function(pt) {
                  return [pt[0], '10%']
                }
              },
              title: {
                left: 'center',
                text: ''
              },
              backgroundColor: 'transparent',
              toolbox: {
                feature: {
                  dataZoom: {
                    yAxisIndex: 'none'
                  },
                  restore: {},
                  saveAsImage: {}
                }
              },
              xAxis: {
                type: 'time',
                boundaryGap: false
              },
              yAxis: {
                type: 'value',
                boundaryGap: [0, '100%']
              },
              dataZoom: [
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                },
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                }
              ],
              series: [
                {
                  name: 'test data',
                  type: 'line',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {},
                  data: dialogChartDataUsage
                }
              ]
            }
            dialogChart.setOption(dialogChartOption)
          })
        }
      })
    },
    async makeManufacturerChart() {
      const manufacturer_chart = echarts.init(document.getElementById('manufacturer_chart'), 'light')
      // 上云部门业务
      let data_manufacturer
      await testApi.getManufacturerChartData().then((res) => {
        data_manufacturer = res.data
      }).catch(() => {
        data_manufacturer = [
          { orgId: 0, orgName: 'aaa', num: 1033 },
          { orgId: 11, orgName: 'bbb', num: 2890 },
          { orgId: 106, orgName: 'ccc', num: 388 },
          { orgId: 107, orgName: 'ddd', num: 12 },
          { orgId: 108, orgName: 'eee', num: 38 },
          { orgId: 109, orgName: 'fff', num: 0 }
        ]
      })
      const maxNum = Math.max.apply(Math, data_manufacturer.map(item => item.num))
      data_manufacturer.sort((a, b) => b.num - a.num)
      const manufacturer_option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        yAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        xAxis: {
          type: 'category',
          data: data_manufacturer.map(s => s.orgName).slice(0, 5),
          axisLabel: {
            color: 'rgb(255,255,255)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            }
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [
          {
            // shadow
            type: 'bar',
            itemStyle: {
              normal: { color: 'rgb(255, 255, 255, 0.2)' }
            },
            data: [maxNum, maxNum, maxNum, maxNum, maxNum],
            barGap: '-100%',
            barWidth: 12
          },
          {
            data: data_manufacturer.map(s => s.num).slice(0, 5),
            type: 'bar',
            barWidth: 12,
            itemStyle: {
              normal: {
                label: {
                  show: true, // 开启显示
                  position: 'bottom',
                  offset: [0, -160],
                  textStyle: { // 数值样式
                    color: '#ffffff',
                    fontSize: 12
                  }
                },
                color: '#042252'
              }
            }
          }],
        grid: {
          containLabel: true,
          left: -10,
          right: 10,
          bottom: 10
        }
      }
      manufacturer_chart.setOption(manufacturer_option)
    },
    async makePieChart() {
      // 主机数
      await testApi.getAbnormalDataList().then((res) => {
        this.abnormalData = res.data
      }).catch(() => {
        this.abnormalData = [
          { name: '阿里云',
            abnormalCpuNum: 2,
            abnormalMemoryNum: 5,
            abnormalDiskNum: 3,
            abnormalCpuList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalMemoryList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 3, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 4, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalDiskList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 33.87 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 10.45 %' }
            ]
          },
          { name: '华为云',
            abnormalCpuNum: 3,
            abnormalMemoryNum: 2,
            abnormalDiskNum: 8,
            abnormalCpuList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalMemoryList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalDiskList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 3, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 4, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 5, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 6, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 7, message: '满足告警触发公式,当前采集值 15.52 %' }
            ]
          },
          { name: '联通云',
            abnormalCpuNum: 8,
            abnormalMemoryNum: 1,
            abnormalDiskNum: 4,
            abnormalCpuList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 3, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 4, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 5, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 6, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 7, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalMemoryList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalDiskList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 3, message: '满足告警触发公式,当前采集值 15.52 %' }
            ]
          },
          {
            name: '火山云',
            abnormalCpuNum: 3,
            abnormalMemoryNum: 1,
            abnormalDiskNum: 3,
            abnormalCpuList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalMemoryList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' }
            ],
            abnormalDiskList: [
              { id: 0, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 1, message: '满足告警触发公式,当前采集值 15.52 %' },
              { id: 2, message: '满足告警触发公式,当前采集值 15.52 %' }
            ]
          }
        ]
      })
      const pieChart = echarts.init(document.getElementById('abnormal_chart'), 'light')
      const data = []
      switch (this.abnormalRadio) {
        case 'CPU':
          for (let i = 0; i < this.abnormalData.length; i++) {
            data.push({ value: this.abnormalData[i].abnormalCpuNum,
              name: this.abnormalData[i].name,
              abnormalList: this.abnormalData[i].abnormalCpuList,
              type: 'CPU'
            },
            )
          }
          break
        case '内存':
          for (let i = 0; i < this.abnormalData.length; i++) {
            data.push({ value: this.abnormalData[i].abnormalMemoryNum,
              name: this.abnormalData[i].name,
              abnormalList: this.abnormalData[i].abnormalMemoryList,
              type: '内存'
            })
          }
          break
        case '磁盘':
          for (let i = 0; i < this.abnormalData.length; i++) {
            data.push({ value: this.abnormalData[i].abnormalDiskNum,
              name: this.abnormalData[i].name,
              abnormalList: this.abnormalData[i].abnormalDiskList,
              type: '磁盘' })
          }
          break
      }

      // 3.配置
      const option = {
        series: [
          {
            type: 'pie',
            data: data,
            roseType: 'radius', // 指定为南格尔玫瑰图
            itemStyle: {
              normal: {
                labelLine: {
                  show: true,
                  length: 8
                }
              }
            },
            label: {
              show: true,
              formatter: function(arg) {
                return arg.data.name + '\n' + '异常数：' + arg.data.value + '\n' + arg.percent + '%'
                // return arg.data.name
              }
            }
          }
        ]
      }
      pieChart.setOption(option)
      const self = this

      pieChart.on('click', function(param) {
        // 关掉饼图上次点击的提示框
        // 添加点击事件
        console.log(param.data)
        let tempData = ''
        for (let i = 0; i < param.data.abnormalList.length; i++) {
          tempData = tempData + 'id:  ' + param.data.abnormalList[i].id + ',' + param.data.abnormalList[i].message + '\n'
        }
        self.pieChartAbnormalList = []
        self.pieChartAbnormalList = param.data.abnormalList
        self.pieChartDialogTitle = '异常数据' + '-' + param.data.name + '-' + param.data.type
        self.pieChartDialogVisible = true

        console.log(self.pieChartFlag)
      })
    },

    async makePerformenceChart() {
      const test_chart = echarts.init(document.getElementById('test'), 'light')
      let data
      await testApi.getPerformanceChartData({ type: this.serviceRadio }).then((res) => {
        data = res.data
      }).catch(() => {
        data = [
          { orgId: 0, orgName: 'ali-region22-1', num: 6 },
          { orgId: 11, orgName: 'ali-region22-2', num: 4 },
          { orgId: 106, orgName: 'huawei-region37-1', num: 12 },
          { orgId: 107, orgName: 'unicom-region24-1', num: 0 },
          { orgId: 108, orgName: 'volcano-region33-1', num: 45 },
          { orgId: 109, orgName: 'huawei-region37-2', num: 0 }
        ]
      })
      const maxNum = Math.max.apply(Math, data.map(item => item.num))
      data.sort((a, b) => b.num - a.num)
      const test_option = {
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            return params[0].name
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'value',
          show: false,
          max: 'dataMax'
        },
        yAxis: {
          type: 'category',
          data: data.map(s => s.orgName).slice(0, 5).reverse(),
          axisLabel: {
            color: 'rgb(255, 255, 255, 0.5)',
            formatter(val) {
              if (val.length < 5) {
                return val
              } else {
                return val.substring(0, 4) + '..'
              }
            },
            show: true
          },
          // 去掉刻度线
          axisLine: { 'show': false },
          axisTick: { 'show': false }
        },
        series: [{
          // shadow
          type: 'bar',
          itemStyle: {
            normal: { color: 'rgb(255, 255, 255, 0.1)' }
          },
          data: [maxNum, maxNum, maxNum, maxNum, maxNum],
          barGap: '-100%',
          barWidth: 10
        }, {
          data: data.map(s => s.disk).slice(0, 5).reverse(),
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true, // 开启显示
                position: 'right',
                textStyle: { // 数值样式
                  color: '#5FA4E9',
                  fontSize: 12
                }
              },
              color: '#042252'
            }
          }
        }],
        grid: {
          containLabel: true,
          top: 10,
          left: 10,
          bottom: 10
        }
      }

      test_chart.setOption(test_option)
      test_chart.getZr().on('click', async params => {
        const pointInPixel = [params.offsetX, params.offsetY]
        if (test_chart.containPixel('grid', pointInPixel)) {
          // 点击第几个柱子
          const pointInGrid = test_chart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
          const yIndex = pointInGrid[1]
          data.sort((a, b) => b.num - a.num)
          const orgName = data.map(s => s.orgName).slice(0, 5).reverse()[yIndex]
          let dialogChartDataUsage = []
          /* for (let i = 0; i < this.dialogChartData.length; i++) {
            if (this.dialogChartData[i].orgName === orgName) {
              dialogChartDataUsage = this.dialogChartData[i].dataListPerformance
            }
          }*/

          await testApi.getLineChartByOrgName({ orgName: orgName, type: 'service' }).then((res) => {
            dialogChartDataUsage = res.data
          }).catch(() => {
            dialogChartDataUsage = this.makeData()
          })
          this.chartDialogVisible = true
          this.$nextTick(() => {
            const dialogChart = echarts.init(this.$refs.chartInDialog, 'dark')
            const dialogChartOption = {
              tooltip: {
                trigger: 'axis',
                position: function(pt) {
                  return [pt[0], '10%']
                }
              },
              title: {
                left: 'center',
                text: ''
              },
              backgroundColor: 'transparent',
              toolbox: {
                feature: {
                  dataZoom: {
                    yAxisIndex: 'none'
                  },
                  restore: {},
                  saveAsImage: {}
                }
              },
              xAxis: {
                type: 'time',
                boundaryGap: false
              },
              yAxis: {
                type: 'value',
                boundaryGap: [0, '100%']
              },
              dataZoom: [
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                },
                {
                  type: 'inside',
                  start: 0,
                  end: 20
                }
              ],
              series: [
                {
                  name: 'test data',
                  type: 'line',
                  smooth: true,
                  symbol: 'none',
                  areaStyle: {},
                  data: dialogChartDataUsage
                }
              ]
            }
            dialogChart.setOption(dialogChartOption)
          })
        }
      })
    },

    openTableDetailDialog(row) {
      this.tableDetailMessage = row
      this.tableDetailMessage.messageName = row.message.slice(10, 21)
      this.tableDetailDialogVisible = true
    },
    async getAlarmTableData() {
      await testApi.getAlarmTableData({ type: this.alarmRadio }).then((res) => {
        this.alarmTableDataShow = res.data
      }).catch(() => {
        this.alarmTableDataShow = []
        this.alarmTableData = [
          {
            'id': '5f3cc9c46a514a9a8ffbd8e7ac9515b0',
            'severity': 'Major',
            'firstTime': '2023-06-13 15:06:35',
            'lastTime': null,
            'message': '满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 100 %.',
            'source': null,
            'host': '127.0.0.1',
            'type': null,
            'tags': {},
            'rootCause': null,
            'mergedBy': null,
            'count': 2,
            'manager': null,
            'alertId': 'bb35ab64fa01334a06a13135b157f05b',
            'systemId': 3,
            'sourceName': null,
            'month': 6,
            'affirmedTime': '2023-06-13 15:50:35',
            'affirmedContent': '不满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 0 %.',
            'affirmedUser': 'system',
            'ip': null
          },
          {
            'id': '73717899659d43378714dfd3145da982',
            'severity': 'Major',
            'firstTime': '2023-06-13 13:33:35',
            'lastTime': null,
            'message': '满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 100 %.',
            'source': null,
            'host': '127.0.0.1',
            'type': null,
            'tags': {},
            'rootCause': null,
            'mergedBy': null,
            'count': 2,
            'manager': null,
            'alertId': '4851c52235c508370e27e1e955c0e6e7',
            'systemId': 3,
            'sourceName': null,
            'month': 6,
            'affirmedTime': '2023-06-13 14:16:35',
            'affirmedContent': '不满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 0 %.',
            'affirmedUser': 'system',
            'ip': null
          },
          {
            'id': '355baba65147431fa4f3b10a9b446c03',
            'severity': 'Minor',
            'firstTime': '2023-06-13 12:40:35',
            'lastTime': null,
            'message': '满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 100 %.',
            'source': null,
            'host': '127.0.0.1',
            'type': null,
            'tags': {},
            'rootCause': null,
            'mergedBy': null,
            'count': 2,
            'manager': null,
            'alertId': '338e6d9793fab129f509354515f9e425',
            'systemId': 3,
            'sourceName': null,
            'month': 6,
            'affirmedTime': '2023-06-13 12:49:35',
            'affirmedContent': '不满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 0 %.',
            'affirmedUser': 'system',
            'ip': null
          },
          {
            'id': '209597f2c93a4f27858ad2db4dcd284a',
            'severity': 'Minor',
            'firstTime': '2023-06-13 12:00:35',
            'lastTime': null,
            'message': '满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 100 %.',
            'source': null,
            'host': '127.0.0.1',
            'type': null,
            'tags': {},
            'rootCause': null,
            'mergedBy': null,
            'count': 2,
            'manager': null,
            'alertId': 'd82cbb6b4b0a38df769f7b752f831dbf',
            'systemId': 3,
            'sourceName': null,
            'month': 6,
            'affirmedTime': '2023-06-13 12:29:35',
            'affirmedContent': '不满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 0.02 %.',
            'affirmedUser': 'system',
            'ip': null
          },
          {
            'id': '5be8447db2c246c3b6b4230d2a5a3da6',
            'severity': 'Warning',
            'firstTime': '2023-06-13 11:26:35',
            'lastTime': null,
            'message': '满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 100 %.',
            'source': null,
            'host': '127.0.0.1',
            'type': null,
            'tags': {},
            'rootCause': null,
            'mergedBy': null,
            'count': 2,
            'manager': null,
            'alertId': '8e26bf7ddf68639778f64972c8ba03e9',
            'systemId': 3,
            'sourceName': null,
            'month': 6,
            'affirmedTime': '2023-06-13 11:36:35',
            'affirmedContent': '不满足告警触发公式：{10.4.78.100:zabbix[process,discoverer,avg,busy].avg(10m)}>75,当前采集值 0 %.',
            'affirmedUser': 'system',
            'ip': null
          }
        ]
        switch (this.alarmRadio) {
          case '全部':
            this.alarmTableDataShow = this.alarmTableData
            break
          case '严重':
            for (let i = 0; i < this.alarmTableData.length; i++) {
              if (this.alarmTableData[i].severity === 'Major') {
                this.alarmTableDataShow.push(this.alarmTableData[i])
              }
            }
            break
          case '中等':
            for (let i = 0; i < this.alarmTableData.length; i++) {
              if (this.alarmTableData[i].severity === 'Minor') {
                this.alarmTableDataShow.push(this.alarmTableData[i])
              }
            }
            break
          case '一般':
            for (let i = 0; i < this.alarmTableData.length; i++) {
              if (this.alarmTableData[i].severity === 'Warning') {
                this.alarmTableDataShow.push(this.alarmTableData[i])
              }
            }
            break
        }
      })
    },
    getSeverityText(severity) {
      switch (severity) {
        case 'Major':
          return '严重'
        case 'Minor':
          return '中等'
        case 'Warning':
          return '一般'
      }
    },
    async getBaseDatas() {
      // 主机数
      await testApi.getHostNums().then((res) => {
        this.hostNum = res.data.hostNum
        this.normalHostNum = res.data.normalHostNum
        this.abnormalHostNum = res.data.abnormalHostNum
      }).catch(() => {
        this.hostNum = 30
        this.normalHostNum = 25
        this.abnormalHostNum = 5
      })
      // 大屏资源概览8项
      await testApi.getGailanInfo().then((res) => {
        this.gailan_box = res.data
      }).catch(() => {
        this.gailan_box = [
          { label: ['云主机概览'], value: '26', total: '30', icon: '/static/hostPerformance/云平台.png' },
          { label: ['CPU概览'], value: '23', total: '30', icon: '/static/hostPerformance/VCPU.png' },
          { label: ['内存概览'], value: '28', total: '30', icon: '/static/hostPerformance/内存.png' },
          { label: ['磁盘概览'], value: '133', total: '150', icon: '/static/hostPerformance/存储.png' },
          { label: ['阿里云'], value: '7', total: '30', icon: '/static/hostPerformance/阿里云.png' },
          { label: ['华为云'], value: '8', total: '30', icon: '/static/hostPerformance/华为云.png' },
          { label: ['联通云'], value: '9', total: '30', icon: '/static/hostPerformance/联通云.png' },
          { label: ['火山云'], value: '6', total: '30', icon: '/static/hostPerformance/火山云.png' }
        ]
      })
    },
    async makeUsageCharts() {
      await this.makeManufacturerChart()
      await this.makePieChart()
      await this.makeCpuChart()
      await this.makeDiskChart()
      await this.makeMemoryChart()
      await this.makePerformenceChart()
    }
  }
}
